<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lostpass extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
		$this->load->helper('form');
	}

	function index()
	{
		$this->load->helper('url');
		$this->load->library('Menu');
		$menu = new Menu;
		
		$data = $menu->gen();
		
		$this->load->view('lostpass', $data);
	}
	
	function send()
	{
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('Menu');
		$this->load->helper('security');
		$this->load->library('email');
		
		// intial email
		$mailconf['mailtype'] = 'html';
		$this->email = new CI_Email($mailconf);
		
		$menu = new Menu;
		$user =& Users::getInstance();
		
		$data = $menu->gen();
		
		$email = $this->input->post('mail');
		$this->db->where('email',$email);
		$q = $this->db->get('users');
		$user = $q->row();
		if ($user) {
			$password = substr(md5(time()), 0, 6);
			$this->db->where('uid', $user->uid);
			$this->db->update('users',array(
				'password'		=>	do_hash($password, 'md5')
			));
			
			$this->email->from('ivy@bookabus.sg', 'Ivy');
			$this->email->to($user->email);
			
			$this->email->subject('Bookabus.sg - Password Retreival');
			$this->email->message('Hello, '.$user->name.',
				<br /><br/>
				You have requested a password retrieval.
				<br /><br/>
				Your password is: '.$password.'
				<br />You can sign in your account {unwrap}'.anchor('/sign-in', 'here').'{/unwrap}.
				<br /><b r/>
				If you haven\'t authorised this password retrieval, please contact us.
				<br /><br />
				Best Regards,
				<br />Ivy');
			
			$this->email->send();
			//echo $this->email->print_debugger();
			
			$message = '<p>Your new password has been e-mailed to you.</p>';
		} else {
			$message = '<p>User not found.</p>';
		}
		
		$data['lpmessage'] = $message;
		
		$this->load->view('lostpass-msg', $data);
	}
}

/* End of file user.php */
/* Location: ./application/controllers/user.php */